//
//  ResultsPage.m
//  AskApp
//
//  Created by Gordon Liu on 11-8-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ResultsPage.h"
#import "QuestionTableCell.h"


@implementation ResultsPage
@synthesize tableView;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if (_refreshHeaderView == nil) {
		
		EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
		view.delegate = self;
		[self.tableView addSubview:view];
		_refreshHeaderView = view;
		[view release];
		
	}
	//  update the last update date
	[_refreshHeaderView refreshLastUpdatedDate];
    
}

- (void)viewDidUnload
{
    _refreshHeaderView=nil;
    [self setTableView:nil];
    [tabImage release];
    tabImage = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//UITableViewDelegate

- (void)dealloc {
    [tableView release];
    [tabImage release];
    [super dealloc];
}

#pragma mark Table view methods
-(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 60;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return	5;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        QuestionTableCell *qview = [[QuestionTableCell alloc] initWithNibName:@"QuestionTableCell" bundle:nil];
        cell = qview.view;
    }
    return cell;
}

#pragma mark -
#pragma mark Data Source Loading / Reloading Methods

- (void)reloadTableViewDataSource{
	
	//  should be calling your tableviews data source model to reload
	//  put here just for demo
	_reloading = YES;
	//如果网络存在则加载数据
    /*	if ([CheckNetwork isExistenceNetwork]) {
     data = [[MyDataSource fetchLibraryInformation] retain];
     }
     */
	
}

- (void)doneLoadingTableViewData{
	
	//  model should call this when its done loading
	_reloading = NO;
	[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
	[self.tableView reloadData];
	
}


#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{	
	
	[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	
	[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
	
}


#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
	
	[self reloadTableViewDataSource];
	[self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:3.0];
	
}

- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
	
	return _reloading; // should return if data source model is reloading
	
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}


- (IBAction)askButtonPressed:(id)sender {
    UIImage *rectImage = [UIImage imageNamed:@"tab_you_asked.png"];
    tabImage.image = rectImage;
}

- (IBAction)helpedButtonPressed:(id)sender {
    UIImage *rectImage = [UIImage imageNamed:@"tab_you_helped.png"];
    tabImage.image = rectImage;
}
@end
